<script setup lang="ts">
import { ref } from 'vue'
import { useOnline } from './hook'

import { useRenderIcon } from '@/components/ReIcon/src/hooks'
import { useTableHeight } from '@/hooks/useTableHeight'
import Search from '@iconify-icons/ep/search'
import Refresh from '@iconify-icons/ep/refresh'
defineOptions({
  name: 'Online',
})

const formRef = ref()
const {
  loading,
  columns,
  dataList,
  pagination,
  queryParams,
  showSelection,
  onSearch,
  resetForm,
  handleForceLogout,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
} = useOnline()
const { tableHeight, tableRef } = useTableHeight()
</script>

<template>
  <div class="main table-view-layout">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryParams"
      class="bg-el-bg w-[99/100] pl-8 pt-4"
    >
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          style="width: 200px"
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 200px"
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)"> 重置 </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar title="在线用户管理">
      <template #buttons />
      <template #table>
        <pure-table
          ref="tableRef"
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :max-height="tableHeight"
          :checkList="showSelection ? ['勾选列'] : []"
          :data="dataList"
          :columns="columns"
          :pagination="pagination"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)',
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button class="reset-margin" link type="primary" @click="handleForceLogout(row)">
              强退
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
